package cn.iocoder.yudao.module.cont.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cont.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.BaseInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.DetailInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.MaterialInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.PayInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.ChangeInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.PlanInfoDO;
import cn.iocoder.yudao.module.cont.service.base.BaseInfoService;

@Tag(name = "管理后台 - 合同基础信息表 ")
@RestController
@RequestMapping("/cont/base-info")
@Validated
public class BaseInfoController {

    @Resource
    private BaseInfoService baseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建合同基础信息表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:create')")
    public CommonResult<Long> createBaseInfo(@Valid @RequestBody BaseInfoSaveReqVO createReqVO) {
        return success(baseInfoService.createBaseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同基础信息表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:update')")
    public CommonResult<Boolean> updateBaseInfo(@Valid @RequestBody BaseInfoSaveReqVO updateReqVO) {
        baseInfoService.updateBaseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同基础信息表 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:base-info:delete')")
    public CommonResult<Boolean> deleteBaseInfo(@RequestParam("id") Long id) {
        baseInfoService.deleteBaseInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同基础信息表 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
    public CommonResult<BaseInfoRespVO> getBaseInfo(@RequestParam("id") Long id) {
        BaseInfoDO baseInfo = baseInfoService.getBaseInfo(id);
        return success(BeanUtils.toBean(baseInfo, BaseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同基础信息表 分页")
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
    public CommonResult<PageResult<BaseInfoRespVO>> getBaseInfoPage(@Valid BaseInfoPageReqVO pageReqVO) {
        PageResult<BaseInfoDO> pageResult = baseInfoService.getBaseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同基础信息表  Excel")
    @PreAuthorize("@ss.hasPermission('cont:base-info:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseInfoExcel(@Valid BaseInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseInfoDO> list = baseInfoService.getBaseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同基础信息表 .xls", "数据", BaseInfoRespVO.class,
                        BeanUtils.toBean(list, BaseInfoRespVO.class));
    }

    // ==================== 子表（建设合同详细信息） ====================

    @GetMapping("/detail-info/page")
    @Operation(summary = "获得建设合同详细信息分页")
    @Parameter(name = "htid", description = "合同编号")
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
    public CommonResult<PageResult<DetailInfoDO>> getDetailInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("htid") String htid) {
        return success(baseInfoService.getDetailInfoPage(pageReqVO, htid));
    }

    @PostMapping("/detail-info/create")
    @Operation(summary = "创建建设合同详细信息")
    @PreAuthorize("@ss.hasPermission('cont:base-info:create')")
    public CommonResult<Long> createDetailInfo(@Valid @RequestBody DetailInfoDO detailInfo) {
        return success(baseInfoService.createDetailInfo(detailInfo));
    }

    @PutMapping("/detail-info/update")
    @Operation(summary = "更新建设合同详细信息")
    @PreAuthorize("@ss.hasPermission('cont:base-info:update')")
    public CommonResult<Boolean> updateDetailInfo(@Valid @RequestBody DetailInfoDO detailInfo) {
        baseInfoService.updateDetailInfo(detailInfo);
        return success(true);
    }

    @DeleteMapping("/detail-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除建设合同详细信息")
    @PreAuthorize("@ss.hasPermission('cont:base-info:delete')")
    public CommonResult<Boolean> deleteDetailInfo(@RequestParam("id") Long id) {
        baseInfoService.deleteDetailInfo(id);
        return success(true);
    }

	@GetMapping("/detail-info/get")
	@Operation(summary = "获得建设合同详细信息")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
	public CommonResult<DetailInfoDO> getDetailInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getDetailInfo(id));
	}

    // ==================== 子表（合同资料表 ） ====================

    @GetMapping("/material-info/page")
    @Operation(summary = "获得合同资料表 分页")
    @Parameter(name = "htid", description = "合同编号")
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
    public CommonResult<PageResult<MaterialInfoDO>> getMaterialInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("htid") String htid) {
        return success(baseInfoService.getMaterialInfoPage(pageReqVO, htid));
    }

    @PostMapping("/material-info/create")
    @Operation(summary = "创建合同资料表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:create')")
    public CommonResult<Long> createMaterialInfo(@Valid @RequestBody MaterialInfoDO materialInfo) {
        return success(baseInfoService.createMaterialInfo(materialInfo));
    }

    @PutMapping("/material-info/update")
    @Operation(summary = "更新合同资料表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:update')")
    public CommonResult<Boolean> updateMaterialInfo(@Valid @RequestBody MaterialInfoDO materialInfo) {
        baseInfoService.updateMaterialInfo(materialInfo);
        return success(true);
    }

    @DeleteMapping("/material-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除合同资料表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:delete')")
    public CommonResult<Boolean> deleteMaterialInfo(@RequestParam("id") Long id) {
        baseInfoService.deleteMaterialInfo(id);
        return success(true);
    }

	@GetMapping("/material-info/get")
	@Operation(summary = "获得合同资料表 ")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
	public CommonResult<MaterialInfoDO> getMaterialInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getMaterialInfo(id));
	}

    // ==================== 子表（合同收付款记录表 ） ====================

    @GetMapping("/pay-info/page")
    @Operation(summary = "获得合同收付款记录表 分页")
    @Parameter(name = "htid", description = "合同编号")
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
    public CommonResult<PageResult<PayInfoDO>> getPayInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("htid") String htid) {
        return success(baseInfoService.getPayInfoPage(pageReqVO, htid));
    }

    @PostMapping("/pay-info/create")
    @Operation(summary = "创建合同收付款记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:create')")
    public CommonResult<Long> createPayInfo(@Valid @RequestBody PayInfoDO payInfo) {
        return success(baseInfoService.createPayInfo(payInfo));
    }

    @PutMapping("/pay-info/update")
    @Operation(summary = "更新合同收付款记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:update')")
    public CommonResult<Boolean> updatePayInfo(@Valid @RequestBody PayInfoDO payInfo) {
        baseInfoService.updatePayInfo(payInfo);
        return success(true);
    }

    @DeleteMapping("/pay-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除合同收付款记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:delete')")
    public CommonResult<Boolean> deletePayInfo(@RequestParam("id") Long id) {
        baseInfoService.deletePayInfo(id);
        return success(true);
    }

	@GetMapping("/pay-info/get")
	@Operation(summary = "获得合同收付款记录表 ")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
	public CommonResult<PayInfoDO> getPayInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getPayInfo(id));
	}

    // ==================== 子表（合同变更记录表 ） ====================

    @GetMapping("/change-info/page")
    @Operation(summary = "获得合同变更记录表 分页")
    @Parameter(name = "htid", description = "合同编号")
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
    public CommonResult<PageResult<ChangeInfoDO>> getChangeInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("htid") String htid) {
        return success(baseInfoService.getChangeInfoPage(pageReqVO, htid));
    }

    @PostMapping("/change-info/create")
    @Operation(summary = "创建合同变更记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:create')")
    public CommonResult<Long> createChangeInfo(@Valid @RequestBody ChangeInfoDO changeInfo) {
        return success(baseInfoService.createChangeInfo(changeInfo));
    }

    @PutMapping("/change-info/update")
    @Operation(summary = "更新合同变更记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:update')")
    public CommonResult<Boolean> updateChangeInfo(@Valid @RequestBody ChangeInfoDO changeInfo) {
        baseInfoService.updateChangeInfo(changeInfo);
        return success(true);
    }

    @DeleteMapping("/change-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除合同变更记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:delete')")
    public CommonResult<Boolean> deleteChangeInfo(@RequestParam("id") Long id) {
        baseInfoService.deleteChangeInfo(id);
        return success(true);
    }

	@GetMapping("/change-info/get")
	@Operation(summary = "获得合同变更记录表 ")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
	public CommonResult<ChangeInfoDO> getChangeInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getChangeInfo(id));
	}

    // ==================== 子表（合同收付款计划表 ） ====================

    @GetMapping("/plan-info/page")
    @Operation(summary = "获得合同收付款计划表 分页")
    @Parameter(name = "htid", description = "合同编号")
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
    public CommonResult<PageResult<PlanInfoDO>> getPlanInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("htid") String htid) {
        return success(baseInfoService.getPlanInfoPage(pageReqVO, htid));
    }

    @PostMapping("/plan-info/create")
    @Operation(summary = "创建合同收付款计划表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:create')")
    public CommonResult<Long> createPlanInfo(@Valid @RequestBody PlanInfoDO planInfo) {
        return success(baseInfoService.createPlanInfo(planInfo));
    }

    @PutMapping("/plan-info/update")
    @Operation(summary = "更新合同收付款计划表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:update')")
    public CommonResult<Boolean> updatePlanInfo(@Valid @RequestBody PlanInfoDO planInfo) {
        baseInfoService.updatePlanInfo(planInfo);
        return success(true);
    }

    @DeleteMapping("/plan-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除合同收付款计划表 ")
    @PreAuthorize("@ss.hasPermission('cont:base-info:delete')")
    public CommonResult<Boolean> deletePlanInfo(@RequestParam("id") Long id) {
        baseInfoService.deletePlanInfo(id);
        return success(true);
    }

	@GetMapping("/plan-info/get")
	@Operation(summary = "获得合同收付款计划表 ")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:base-info:query')")
	public CommonResult<PlanInfoDO> getPlanInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getPlanInfo(id));
	}

}